<template>
  <header class="header">
    <nav class="header-container clearfix">
      <!--移动端-->
      <button
        type="button"
        class="navbar-toggle collapsed"
        @click="toggleMenu()"
      >
        <q-icon name="mdi-menu-open"></q-icon>
      </button>

      <!-- logo -->
      <div class="navbar-logo-container clearfix">
        <router-link to="/" rel="home" class="navbar-logo">
          <!-- <img
            :src="require('../assets/img/logo.png')"
            style="width: auto;height: 22px;    outline: 0;"
          />-->
          <span>LvZhenBLOG</span>
        </router-link>
      </div>

      <!-- 主菜单 -->

      <div :class="`navbar-main`">
        <ul id="menu" class="nav navbar-nav">
          <template v-for="(menuitem, index) in menus">
            <li
              ref="navmenu"
              v-if="menuitem.isdropdown"
              :key="index"
              :class="[
                dropdownActiveIndex == menuitem.id ? 'open' : '',
                'menu-item dropdown'
              ]"
              @mouseover="toggleDropDown(menuitem, true)"
              @mouseout="toggleDropDown(menuitem, false)"
              @click="showSubMenu(menuitem.id, index)"
            >
              <span class="dropdown-toggle" v-ripple>
                <!-- <i :class="menuitem.icon"></i> -->
                {{ menuitem.text }}
                <img
                  src="https://www.vc99.cn/content/templates/meta/Static/images/svg/arrow.svg"
                  class="arrow"
                />
              </span>
              <transition
                mode="out-in"
                enter-active-class="animate__animated  animate__slideInDown"
                leave-active-class="animate__animated  animate__slideOutUp"
                :duration="500"
              >
                <ul class="dropdown-menu clearfix">
                  <template v-for="(child, index) in menuitem.children">
                    <li :key="index">
                      <router-link class="text-weight-bold" :to="child.path">{{
                        child.text
                      }}</router-link>
                    </li>
                  </template>
                </ul>
              </transition>
            </li>
            <li :key="index" v-else class="menu-item" ref="navmenu">
              <router-link :to="menuitem.path">
                <!-- <i :class="menuitem.icon"></i> -->
                {{ menuitem.text }}
              </router-link>
            </li>
          </template>
        </ul>
      </div>

      <!-- 用户 -->
      <div class="navbar-user  float-right text-weight-bold">
        <span class="login" @click="showLoginDialog()" v-if="!logined">
          <q-icon name="mdi-account-arrow-right" />
          登录</span
        >

        <div class="avatar">
          <q-avatar
            v-if="logined"
            size="32px"
            rounded
            @click="avatarDropdownVisible = true"
          >
            <img :src="profile.avatar" />
          </q-avatar>
          <div
            v-if="avatarDropdownVisible"
            class="dropdown-wrapper"
            @click.self="avatarDropdownVisible = false"
          ></div>
          <div
            id="avatarDropdown"
            class="shadow-5"
            v-if="avatarDropdownVisible"
          >
            <span class="dropdown-item" @click="logout">
              <q-icon name="mdi-logout"> </q-icon>
              退出
            </span>
          </div>
        </div>
      </div>
      <MobileHeader @closeNav="toggleMenu" />
    </nav>
  </header>
</template>
<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import { Category } from 'src/models/category';
import { Menu } from 'src/models/menu';
import MobileHeader from './mobile/header.vue';
// import headCover from "./main/headCover.vue";
@Component({
  components: { MobileHeader }
})
export default class AppHeader extends Vue {
  private categories: Category[] = [];
  private menus: Menu[] = [
    {
      id: 1,
      icon: 'fa fa-archive',

      text: '代码',
      path: '/category/代码',
      isdropdown: false,
      children: []
    },
    {
      id: 2,
      icon: 'fa fa-archive',

      text: '日常',
      path: '/category/日常',
      isdropdown: false,
      children: []
    },
    {
      id: 10,
      icon: 'fa fa-archive',

      text: '归档',
      path: '/archive',
      isdropdown: false,
      children: []
    },
    {
      id: 11,
      text: '关于',
      path: '/about',
      icon: 'fa fa-info',
      isdropdown: false,
      children: []
    }
  ];

  private avatarDropdownVisible: boolean = false;

  //展开 移动端菜单
  toggleMenu(): void {
    if (document.body.parentElement?.classList.contains('nav-on')) {
      document.body.parentElement?.classList.remove('nav-on');
    } else {
      document.body.parentElement?.classList.add('nav-on');
    }
  }

  //登录弹窗
  showLoginDialog() {
    this.$store.commit('global/SET_LOGIN_DIALOG_VISIBLE', true);
  }

  get logined() {
    return this.$store.getters['login/accessToken'];
  }
  get profile() {
    return this.$store.getters['login/userProfile'];
  }

  logout() {
    this.avatarDropdownVisible = false;
    this.$store.dispatch('login/logout');
  }
}
</script>
<style lang="scss">
#mobile-overlay {
  transition: 0.5s;
}
</style>

<style lang="scss" scoped>
.header {
  position: fixed;
  top: 0;
  z-index: $z-index-header;
  width: 100%;
  border: 0;
  transition: $transition-time-slow;
  height: $header-height;
  display: flex;
  justify-content: center;
  align-items: center;
  .avatar {
    position: relative;
    text-align: center;
    .dropdown-wrapper {
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      z-index: 10000;
    }
    #avatarDropdown {
      position: absolute;
      top: 60px;
      line-height: 30px;
      left: 0px;
      min-width: 60px;
      font-size: 12px;
      border-radius: 5px;
      z-index: 10001;
    }
  }
  &.nobg {
    background: #fff;

    box-shadow: 0 0 10px 0px rgba(0, 0, 0, 0.1);
    height: 35px;
    color: black;
    .header-container {
      .navbar-toggle {
        i {
          color: rgba(0, 0, 0, 0.8);
        }
      }
      a {
        color: black;
      }
      .menu-item a {
        color: black;
      }

      .avatar #avatarDropdown {
        background: rgba(0, 0, 0, 0.5);
        color: white;
      }
    }
  }
  &.hasbg {
    background: transparent;
    color: $module-bg;

    .header-container {
      padding: 5px;

      .navbar-logo-container {
        .navbar-logo {
          span {
            color: white;
            &::before {
              width: 0;
            }
          }
        }
      }
      .navbar-main {
        .menu-item {
          a {
            color: $module-bg;
          }
          .nuxt-link-exact-active.nuxt-link-active {
            color: lightgreen;
          }
        }
      }
      .avatar #avatarDropdown {
        background: white;
        color: black;
      }
    }
  }

  .header-container {
    width: 1200px;
    padding: 10px;
    position: relative;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: $header-height;
    transition: $transition-time-slow;
    .navbar-logo-container {
      display: flex;
      position: relative;
      align-items: center;
      .navbar-logo {
        span {
          font-size: 20px;
          font-weight: 900;
          letter-spacing: 2px;
          color: rgb(52, 73, 94);
          &:before {
            content: '';
            position: absolute;
            width: 60px;
            height: 10px;
            background-color: rgba(255, 255, 255, 0.5);
            -webkit-transform: rotate(-45deg);
            transform: rotate(-45deg);
            -webkit-animation: searchLights 1s ease-in 1s infinite;
            animation: searchLights 1s ease-in 1s infinite;
          }
        }
      }
    }
    .navbar-main {
      display: flex;
      align-items: center;
      height: 100%;
      .navbar-nav {
        display: block;

        .menu-item {
          display: inline-block;
          list-style: none;
          padding: 10px;
          cursor: pointer;
          a {
            font-weight: bold;
            opacity: 0.8;
            &:hover {
              opacity: 1;
              color: lightgreen;
              &::before {
                opacity: 1;
                transform: translateX(0);
              }
              &::after {
                opacity: 1;
                transform: translateX(0);
              }
            }
            &::before,
            &::after {
              display: inline-block;
              transition: transform 0.3s, opacity 0.2s;
            }
            &::before {
              margin-right: 5px;
              opacity: 0;
              content: '[';
              transform: translateX(20px);
            }
            &::after {
              opacity: 0;
              margin-left: 5px;
              content: ']';
              transform: translateX(-20px);
            }
            &.router-link-exact-active {
              color: lightgreen;
              opacity: 1;
              &::before {
                opacity: 1;
                transform: translateX(0);
              }
              &::after {
                opacity: 1;
                transform: translateX(0);
              }
            }

            span {
              display: inline-block;
              height: $header-height;
              line-height: $header-height;
              position: relative;
              width: 100%;
              padding: 0 20px;
            }
            i {
              display: none;
            }
            > a {
              display: inline-block;
              height: $header-height;
              line-height: $header-height;
              padding: 0 10px;
            }
          }
        }
      }
    }

    .navbar-user {
      height: $header-height;
      margin-left: 20px;
      min-width: 65px;
      position: relative;
      cursor: pointer;
      display: flex;
      align-items: center;
      .login {
        min-width: 50px;
      }
      .avatar {
        display: inline-block;
        text-align: center;
        width: 100%;
        transition: $transition-time-slow;
        a {
          font-size: 12px;
        }
        &:hover {
          a {
            cursor: pointer;
          }
        }
        &:last-child {
          a {
            cursor: not-allowed;
            pointer-events: none;
          }
        }
      }
    }
    .navbar-toggle {
      display: none;
      position: relative;
      float: left;
      margin-right: 15px;
      padding: 9px 10px;
      margin-top: 8px;
      margin-bottom: 8px;
      background-color: transparent;
      background-image: none;
      border: 1px solid transparent;
      border-radius: 4px;
      cursor: pointer;
      i {
        font-size: 30px;
        color: white;
      }
    }
  }
}

//屏幕小于1024
@media screen and (max-width: 1024px) {
  .header {
    box-shadow: none;
    .header-container {
      width: 980px;
      width: 100%;
      display: block;
      padding: 0 15px;
      text-align: center;
      .navbar-logo-container {
        position: absolute;
        left: 50%;
        height: $header-height;
        transform: translateX(-50%);
        .navbar-logo {
          float: none;

          display: inline-block;
        }
      }
      .navbar-main {
        display: none;
      }
      .navbar-search.active {
        left: auto;
      }
      .header .navbar-header {
        margin: 0;
        float: none;
        text-align: center;
      }
      .navbar-toggle {
        display: block;
        transition: all 0.3s ease-out 0s;
      }
    }
  }
}

//屏幕大于1024
@media screen and (min-width: 1024px) {
  .header {
    &.hasbg {
      .header-container {
        .navbar-nav {
          .dropdown {
            .dropdown-menu {
              background-color: rgba(28, 31, 33, 0.8);
              &:before {
                border-bottom: 6px solid rgba(28, 31, 33, 0.8);
                opacity: 0.8;
              }
              li {
                a {
                  color: $module-bg;
                }
              }
            }
          }
        }
      }
    }
  }
}

//动画
@keyframes searchLights {
  0% {
    left: -90px;
    top: 0;
  }
  to {
    left: 90px;
    top: 0;
  }
}
@keyframes fadeIn {
  from {
    max-height: 0;
  }
  to {
    max-height: 160px;
  }
}
@keyframes test {
  from {
    max-height: 0;
  }
  to {
    max-height: 40px;
  }
}
@keyframes fadeOut {
  from {
    max-height: 160px;
  }
  to {
    max-height: 0;
  }
}
</style>
